//
//  ByUIShopsPaySuccessVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIShopsPaySuccessVC.h"

// vc
#import "ByUIShopsDetailVC.h"

// view
#import "ZKBombView.h"
#import "ByViewShopsEvaluate.h"

// cell
#import "ByCellShopsIndexShopsItem.h"

// api
#import "ByApiShops.h"

// model
#import "ByModelShopsList.h"

@interface ByUIShopsPaySuccessVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView       *table;
@property (nonatomic, strong) NSMutableArray    *items;
@property (nonatomic, strong) UIView            *titleView;
@property (nonatomic, strong) UIButton          *evaluateBtn;
@property (nonatomic, strong) ZKBombView        *evaluatePopView;

@end

@implementation ByUIShopsPaySuccessVC
{
    NSInteger _page;
    BOOL _showDistance;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"付款成功";
    self.view.backgroundColor = [UIColor blueColor];
    [self addLeftBackBarButtonItem];
    
    
    [self createViews];
    
    // 请求附近商家
    [self.table.mj_header beginRefreshing];

}

- (void)viewSafeAreaInsetsDidChange{
    [super viewSafeAreaInsetsDidChange];
    self.titleView.top = self.view.safeAreaInsets.top + 20;
    self.evaluateBtn.top = self.titleView.bottom + 20;
    self.table.top = self.evaluateBtn.bottom + 10;
    self.table.height = self.view.height - self.evaluateBtn.bottom - 10 - self.view.safeAreaInsets.bottom ;
}

#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}
- (ZKBombView *)evaluatePopView {
    
    if (!_evaluatePopView) {
        _evaluatePopView = [[ZKBombView alloc] init];
        _evaluatePopView.popMode = ZKBombPopModeBottom;
        _evaluatePopView.maskTap = false;
        ByViewShopsEvaluate *evaluateView = [[ByViewShopsEvaluate alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
//        __weak typeof(evaluateView) weak = evaluateView;
        evaluateView.cancelBlock = ^ {
            [_evaluatePopView remove];
        };
        __weak typeof(evaluateView) weakView = evaluateView;
        evaluateView.okBlock = ^ {
            NSLog(@"%ld",(long)weakView.level);
//            [ByCoreToolAlert showOnlyText:[NSString stringWithFormat:@"谢谢您的评分"]];
            [self evaluateShop:weakView.level];
            [_evaluatePopView remove];
        };
        _evaluatePopView.contentView = evaluateView;
    }
    
    return _evaluatePopView;
}


#pragma mark - 请求数据
- (void)queryShopsNearby {
    
    ByModelLocationCity *selectedCityModel = BY_APPDELEGATE.selectedCityModel;
    // 判断定位城市和选择城市是否相同
    if (selectedCityModel == nil) {
        _showDistance = YES;
    } else {
        if ([selectedCityModel.city isEqualToString:BY_APPDELEGATE.locationCity]) {
            _showDistance = YES;
        } else {
            _showDistance = NO;
        }
    }
    
    
    ByNetParamsModel *params = [ByNetParamsModel new];
    NSLog(@"%@", BY_APPDELEGATE.locationDic);
    
    
    
    if (selectedCityModel) {
        params.city_code = selectedCityModel.cityID;
    }
    
    params.lng = BY_APPDELEGATE.locationDic[@"lng"];
    params.lat = BY_APPDELEGATE.locationDic[@"lat"];
    params.page_index = [NSString stringWithFormat:@"%li", _page];
    params.page_size = @"10";
    
//    [self performSelector:@selector(loading) withObject:nil afterDelay:0.01];
    
    [ByApiShops queryShopsNearby:params success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        
        ByModelShopsList *listModel = result.data;
        
        if (_page <= 1) {
            
            [self.items removeAllObjects];
        }
        
        [self.items addObjectsFromArray:listModel.list];
        [self.table reloadData];
        
        if (self.items.count == listModel.count.integerValue) {
            
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshingWithNoMoreData];
        } else {
            
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshing];
        }
        
    } failure:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshingWithNoMoreData];
        
    }];
}
- (void)loading {
    
    [ByCoreToolAlert showLoadind:@"正在加载..."];
}

// 评价
- (void)evaluateShop:(NSInteger)level{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.seller_id = self.store_id;
    model.score = [NSString stringWithFormat:@"%ld",level];
    [ByApiShops evaluateShop:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:@"评价成功"];
        [self by_normalPopVC:@"ByUIPayVC"];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
}

#pragma mark - createViews
- (void)createViews {
    
    CGFloat x, y, width, height;
    /*
     付款成功title视图
     */
    x = 10;
    y = 64 + 20;
    width = SCREEN_WIDTH - x * 2;
    height = width * 0.4;
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.view addSubview:_titleView];
    _titleView.layer.cornerRadius = 4;
    _titleView.backgroundColor = [UIColor whiteColor];
    
    x = 30;
    y = 0;
    NSString *text = @"付款成功\n商家会收到您的款项";
    width = 200;
    height = _titleView.height;
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    noteLabel.font = FONT(14);
    noteLabel.numberOfLines = 0;
    noteLabel.textColor = [UIColor grayColor];
    noteLabel.textAlignment = NSTextAlignmentLeft;
    [_titleView addSubview:noteLabel];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:[text rangeOfString:@"付款成功"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(16) range:[text rangeOfString:@"付款成功"]];
    noteLabel.attributedText = attrStr;
    
    
    x = 0;
    y = 0;
    width = 50;
    height = 50;
    UIImageView *noteImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    noteImageView.image = [UIImage imageNamed:@"Shops_pay_success"];
    [_titleView addSubview:noteImageView];
    noteImageView.right = _titleView.width - 30;
    noteImageView.y = _titleView.height / 2;
    
    
    /*
     评价按钮
     */
    x = 10;
    y = _titleView.bottom + 20;
    width = _titleView.width;
    height = 40;
    _evaluateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_evaluateBtn setBackgroundColor:rgba(23, 160, 255, 1)];
    [_evaluateBtn setFrame:CGRectMake(x, y, width, height)];
    [_evaluateBtn setTitle:@"温馨评价" forState:UIControlStateNormal];
    [_evaluateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_evaluateBtn addTarget:self action:@selector(evaluate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_evaluateBtn];
    _evaluateBtn.layer.cornerRadius = height / 2;
    
    
    /*
     附近推荐
     */
    [self createTableView:_evaluateBtn.bottom + 10];

}

// 创建表视图
- (void)createTableView:(CGFloat)top {
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(10, top, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 10 - top) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    UIView *tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.table.width, 40)];
    self.table.tableHeaderView = tableHeader;
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        
        [self queryShopsNearby];
    }];
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        
        _page ++;
        
        [self queryShopsNearby];
    }];
    
    self.table.mj_header = header;
    self.table.mj_footer = footer;
    
    /*
     制作header
     */
    CGFloat x, y, width, height;
    
    x = 0;
    y = 0;
    width = 20;
    height = 20;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    imageView.image = [UIImage imageNamed:@"Shops_star_light"];
    [tableHeader addSubview:imageView];
    
    x = 0;
    y = 0;
    NSString *text = @"推荐附近";
    width = [text getSizeOfString:16].width + 2;
    height = 20;
    UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    noteLabel.font = FONT(16);
    noteLabel.text = text;
    noteLabel.textColor = [UIColor whiteColor];
    noteLabel.textAlignment = NSTextAlignmentLeft;
    [tableHeader addSubview:noteLabel];

    
    CGFloat remain = (tableHeader.width - imageView.width - noteLabel.width - 5) / 2;
    imageView.left = remain;
    imageView.y = tableHeader.height / 2;
    noteLabel.left = imageView.right + 5;
    noteLabel.y = tableHeader.height / 2;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(remain - 5, tableHeader.height / 2)];
    [path addLineToPoint:CGPointMake(remain - 5 - 20, tableHeader.height / 2)];
    [path moveToPoint:CGPointMake(noteLabel.right + 5, tableHeader.height / 2)];
    [path addLineToPoint:CGPointMake(noteLabel.right + 5 + 20, tableHeader.height / 2)];
    CAShapeLayer *line = [CAShapeLayer layer];
    line.path = path.CGPath;
    line.strokeColor = [UIColor whiteColor].CGColor;
    line.lineWidth = 1;
    [tableHeader.layer addSublayer:line];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellShopsIndexShopsItem *cell = [tableView dequeueReusableCellWithIdentifier:@"ByCellShopsIndexShopsItem"];
    if (cell == nil) {
        cell = [[ByCellShopsIndexShopsItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ByCellShopsIndexShopsItem"];
        cell.showDistance = _showDistance;
    }
    cell.model = self.items[indexPath.section];

    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByUIShopsDetailVC *vc = [[ByUIShopsDetailVC alloc] init];
    vc.shopsItemModel = self.items[indexPath.section];
    [self by_normalPush:vc hasNavi:YES];
}


#pragma mark - 点击事件
- (void)leftItemsAction:(NSInteger)leftIndex {
    
    [self by_normalPopVC:@"ByUIShopsIndexVC"];
}
- (void)evaluate {
    
    [self.evaluatePopView show];
}

@end
